package com.ibeeking.gateway.open.service.impl;

import com.ibeeking.gateway.open.constant.RedisKeyConstant;
import com.ibeeking.gateway.open.service.IResourceService;
import com.ibeeking.found.common.config.feign.TenantUserClient;
import com.ibeeking.found.common.constants.HeaderNounConstant;
import com.ibeeking.nematos.redis.utils.RedisUtils;
import com.ibeeking.nematos.utils.data.Collections3;
import com.ibeeking.nematos.utils.json.JsonUtils;
import com.ibeeking.nematos.utils.result.ResponseResult;
import com.alibaba.fastjson.JSONObject;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Service;
import org.springframework.util.AntPathMatcher;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName ResourceServiceImpl
 * @Description 资源服务类
 * @Author ibeeking
 * @Date 2020-12-14 10:33
 **/
@RefreshScope
@Service
public class ResourceServiceImpl implements IResourceService {

    private final static AntPathMatcher antPathMatcher = new AntPathMatcher();

    @Resource
    private RedisUtils redisUtils;
    @Resource
    private TenantUserClient tenantUserClient;

    @Override
    public Boolean checkApi(JSONObject token, String requestPath, ServerHttpRequest request) {
        List<String> apis = loadCurrentUserResource(token, request);
        if (Collections3.isEmpty(apis)) {
            return false;
        }
        for (int i = 0; i < apis.size(); i++) {
            if (antPathMatcher.match(apis.get(i), requestPath)) {
                return true;
            }
        }
        return false;
    }

    public List<String> loadCurrentUserResource(JSONObject token, ServerHttpRequest newRequest) {
        String jti = token.get(HeaderNounConstant.JWT_JTI).toString();
        Object o = redisUtils.get(RedisKeyConstant.ROLE_RESOURCE_LIST + jti);
        if (null != o) {
            return JsonUtils.jsonToList(o, String.class);
        } else {
            String userId = token.get(HeaderNounConstant.USER_ID).toString();
            ResponseResult<List<String>> responseResult = tenantUserClient.queryApis(Long.valueOf(userId), newRequest.getId(), newRequest.getHeaders().getFirst(HeaderNounConstant.TENANT_ID), newRequest.getHeaders().getFirst(HeaderNounConstant.USER_ID), newRequest.getHeaders().getFirst(HeaderNounConstant.USER_NAME), newRequest.getHeaders().getFirst(HeaderNounConstant.LOGIN_ROLE), newRequest.getHeaders().getFirst(HeaderNounConstant.LOGIN_SYSTEM));
            if (null == responseResult || Collections3.isEmpty(responseResult.getData())) {
                List<String> result = Collections3.emptyList();
                redisUtils.set(RedisKeyConstant.ROLE_RESOURCE_LIST + jti, result, 60L);
                return result;
            }
            redisUtils.set(RedisKeyConstant.ROLE_RESOURCE_LIST + jti, responseResult.getData(), RedisKeyConstant.HALF_DAY);
            return responseResult.getData();
        }
    }

}
